package com.ruoyi.project.sysmanage.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.sysmanage.domain.Dept;
import com.ruoyi.project.sysmanage.domain.User;
import com.ruoyi.project.sysmanage.service.SysmConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.jws.soap.SOAPBinding;
import java.util.List;

@RestController
@RequestMapping("/sysmanage/user")
public class SysmConsumerController extends BaseController {

    @Autowired
    private SysmConsumerService sysmConsumerService;
    //删除用户
    @DeleteMapping("/{delUsers}")
    @PreAuthorize("@ss.hasPermi('sysmanage:user:delete')")
    public AjaxResult deleteUsers(@PathVariable int[] delUsers){

        return toAjax(sysmConsumerService.deleteDepts(delUsers));
    }
    //修改用户
    @PutMapping
    @PreAuthorize("@ss.hasPermi('sysmanage:consumer:update')")
    public AjaxResult updateUsers(@RequestBody User user){
        //设置修改人姓名
        user.setUpdateBy(SecurityUtils.getUsername());
        int rows = sysmConsumerService.updateUser(user);
        return null;
    }

    //查询增加条件
    @GetMapping("/all")
    public AjaxResult getAll(){
        return AjaxResult.success(sysmConsumerService.getAll());
    }

    //新增用户
    @PostMapping()
    @PreAuthorize("@ss.hasPermi('sysmanage:user:add')")
    public AjaxResult addUser(@RequestBody User user){

        user.setCreateBy(SecurityUtils.getUsername());
        int rows = sysmConsumerService.insertOne(user);

        return toAjax(rows);
    }
    //查询所有用户
    @PreAuthorize("@ss.hasPermi('sysmanage:user:query')")
    @GetMapping("/find")
    public TableDataInfo find(User user){

        startPage();
        List<User> list =  sysmConsumerService.findAll(user);
        return getDataTable(list);
    }

    /**
     * 根据部门编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('sysmanage:consumer:query')")
    @GetMapping("/{userId}")
    public AjaxResult getById(@PathVariable int userId)
    {
       User user = sysmConsumerService.getById(userId);
        return AjaxResult.success(user);
    }
}
